home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / DCLAP 4j / DBio / DSeqCmds.cpp < prev    next >
Encoding:
Text File  |  1995-12-17  |  21.7 KB  |  976 lines  |  [TEXT/R*ch]

  1. // DSeqCmds.cp
  2.  
  3.  
  4. #include <ncbi.h>
  5. #include <DTask.h>
  6. #include <DApplication.h>
  7. #include <DClipboard.h>
  8. #include "DSeqDoc.h"
  9. #include "DSeqEd.h"
  10. #include "DSeqCmds.h"
  11.  
  12.  
  13.  
  14. static Nlm_PoinT gZeroPt = { 0, 0 };
  15.  
  16.  
  17. // DSeqChangeCmd : public DCommand ----------------------------
  18.  
  19. DSeqChangeCmd::DSeqChangeCmd(char* title, DSeqDoc* itsAlnDoc, DView* itsView, DSeqList* itsSeqs) :
  20.     DCommand( cSeqChange, itsView, title, DCommand::kCanUndo, DCommand::kCausesChange)
  21. {
  22.     fOldSeqs= NULL;
  23.     fNewSeqs= NULL;
  24.     fAlnView= itsAlnDoc->fAlnView;
  25.     fSeqDoc= itsAlnDoc;
  26.     //fAlnView->DeinstallEditSeq(); //! prevent mangle do to fBases newhandle 
  27.     
  28.     //- gApplication->CommitLastCommand(); -- Paste seqs is giving probs...
  29.     // theContext:    TCommandHandler;
  30.     //theContext= itsView->GetContext(cSeqChange);
  31.     //ICommand(cSeqChange, theContext, kCanUndo, kCausesChange, theContext);
  32.  
  33. #if 0
  34.         // let user fix fOldSeqs !?
  35.     if (itsSeqs == NULL)  {
  36.         long start, nbases;
  37.         itsAlnDoc->GetSelection( TRUE, TRUE, itsSeqs, start, nbases);
  38.         }
  39. #endif
  40.     fOldSeqs= itsSeqs;
  41.     fNewSeqs= new DSeqList(); 
  42.     
  43.     // caller must call Initialize() before other use of this object
  44. }    
  45.  
  46.  
  47. DSeqChangeCmd::~DSeqChangeCmd() 
  48. {
  49.     if (fNewSeqs) {
  50.         fNewSeqs->DeleteAll();         //forget objects -- doesn't affect objects 
  51.         delete fNewSeqs;              
  52.         }
  53.     if (fOldSeqs) {
  54.         fOldSeqs->FreeAllObjects();     //drop all objects && list
  55.         delete fOldSeqs;
  56.         }
  57. }
  58.  
  59. Boolean DSeqChangeCmd::Initialize()  
  60. {
  61.     if (fOldSeqs) {
  62.         short i, n= fOldSeqs->GetSize();
  63.         for (i=0; i<n; i++) {
  64.             DSequence* oldSeq= fOldSeqs->SeqAt(i);
  65.             DSequence* newSeq = ChangeToNew(oldSeq);
  66.             if (newSeq) 
  67.                 fNewSeqs->InsertLast( newSeq);
  68.             else  
  69.                 return false; // fail 
  70.             }
  71.         }
  72.     return true;
  73. }
  74.  
  75. void DSeqChangeCmd::DoIt()  
  76. {
  77.     DCommand::DoIt();
  78.     fSeqDoc->Dirty();
  79.     DoItWork();
  80. }
  81.  
  82. void DSeqChangeCmd::Undo()  
  83. {
  84.     DCommand::Undo();
  85.     fSeqDoc->UnDirty();
  86.     UndoWork();
  87. }
  88.  
  89. void DSeqChangeCmd::Redo()  
  90. {
  91.     DCommand::Redo();
  92.     fSeqDoc->Dirty();
  93.     UndoWork();
  94. }
  95.  
  96. void DSeqChangeCmd::UndoWork()  
  97. {
  98.     DSeqList* tempList= fNewSeqs;
  99.     fNewSeqs= fOldSeqs;
  100.     fOldSeqs= tempList;
  101.     DoItWork();
  102. }
  103.  
  104.  
  105.  
  106. void DSeqChangeCmd::DoItWork()  
  107. {
  108.     short                i, j, nseqs, aFromRow, aToRow, indx, saveEdit;
  109.     DSequence        * oldSeq, * newSeq;
  110.     DSeqList         *    fullList;
  111.     
  112.     if (fOldSeqs && fNewSeqs) {
  113.         saveEdit= fAlnView->fEditRow;        
  114.         aFromRow= kEmptyIndex;
  115.         aToRow= kEmptyIndex;
  116.         nseqs= fNewSeqs->GetSize();
  117.         fullList= fAlnView->fSeqList;
  118.         for (i= 0; i<nseqs; i++) {
  119.             newSeq= fNewSeqs->SeqAt(i);
  120.             oldSeq= fOldSeqs->SeqAt(i);
  121. #if 1
  122.             long k, nfull= fullList->GetSize();
  123.             if (oldSeq) for (k=0, j= kEmptyIndex; k<nfull; k++) {
  124.                 DSequence* aseq= fullList->SeqAt(k);
  125.                 if (aseq && aseq->Checksum() == oldSeq->Checksum() 
  126.                     && aseq->LengthF() == oldSeq->LengthF()
  127.                     && aseq->ModTime() == oldSeq->ModTime()
  128.                     ) {
  129.                         j= k;
  130.                         break;
  131.                         }
  132.                 }
  133. #else
  134.             j= fullList->GetEqualItemNo( oldSeq); // this doesn't work, default equal item tests ptr val
  135. #endif
  136.             if (j>kEmptyIndex) {
  137.                 fullList->AtDelete(j); //fullList->Delete( oldSeq);
  138.                 fullList->InsertBefore( j, newSeq);
  139.                 if (aFromRow == kEmptyIndex) aFromRow= j;
  140.                 else aToRow= j;
  141.                 }    
  142.             DSeqedWindow::UpdateEdWinds( oldSeq, newSeq);
  143.             }
  144.         
  145.         if (aToRow==kEmptyIndex) aToRow= aFromRow;
  146.         
  147.         nseqs= fullList->GetSize();
  148.         for (i=0, indx= 0; i<nseqs; i++) {
  149.             oldSeq= fullList->SeqAt(i);
  150.             if (indx >= aFromRow && indx <= aToRow) {
  151.                 //! also need to shift any selection or deselect all...
  152.                 Nlm_RecT vLoc;
  153.                 fAlnView->GetRowRect( indx, vLoc, 1);
  154.                 fAlnView->InvalRect( vLoc);
  155.                 if (indx == saveEdit) {
  156.                     fAlnView->fEditRow= saveEdit;
  157. #if 0
  158.                     fAlnView->fEditSeq->Locate( vLoc.topLeft, true);
  159.                     vSize= fAlnView->fEditSeq.fSize;
  160.                     fAlnView->fEditSeq->Resize( vSize, false);  
  161. #endif
  162.                     }
  163.                     
  164.                 // ??? isn't this already done above
  165.                 //fAlnView->GetCellRect( indx, 1, vLoc);
  166.                 //fAlnView->InvalRect( vLoc);
  167.                 }
  168.             indx++;
  169.             }
  170.             
  171.         //- fAlnView->SetEmptySelection(kHighlight); 
  172.         //fCommandDone= true; 
  173.         }
  174. }
  175.  
  176. void DSeqChangeCmd::Commit()  
  177. {
  178.     DCommand::Commit(); 
  179.     //this->DoNotification();
  180. }
  181.  
  182.  
  183. DSequence* DSeqChangeCmd::ChangeToNew( DSequence* oldSeq) 
  184. {     
  185.     return (DSequence*) oldSeq->Clone();
  186. }
  187.  
  188. DSequence* DSeqReverseCmd::ChangeToNew( DSequence* oldSeq) 
  189. {     
  190.     return oldSeq->Reverse();
  191. }
  192.  
  193. DSequence* DSeqComplementCmd::ChangeToNew( DSequence* oldSeq) 
  194. {     
  195.     return oldSeq->Complement();
  196. }
  197.  
  198. DSequence* DSeqRevComplCmd::ChangeToNew( DSequence* oldSeq) 
  199. {     
  200.     DSequence* tempSeq= oldSeq->Reverse();  
  201.     DSequence* result= tempSeq->Complement();
  202.     delete tempSeq;  
  203.     return result;
  204. }
  205.  
  206.  
  207. DSequence* DSeqCompressCmd::ChangeToNew( DSequence* oldSeq) 
  208. {     
  209.     return oldSeq->Compress();
  210. }
  211.  
  212. DSequence* DSeqDna2RnaCmd::ChangeToNew( DSequence* oldSeq) 
  213. {     
  214.     return oldSeq->Dna2Rna(TRUE);
  215. }
  216.  
  217. DSequence* DSeqRna2DnaCmd::ChangeToNew( DSequence* oldSeq) 
  218. {     
  219.     return oldSeq->Dna2Rna(FALSE);
  220. }
  221.  
  222. DSequence* DSeqTranslateCmd::ChangeToNew( DSequence* oldSeq) 
  223. {     
  224.     return oldSeq->Translate();
  225. }
  226.  
  227. DSequence* DSeqLockIndelsCmd::ChangeToNew( DSequence* oldSeq) 
  228. {     
  229.     return oldSeq->LockIndels(TRUE);
  230. }
  231.  
  232. DSequence* DSeqUnlockIndelsCmd::ChangeToNew( DSequence* oldSeq) 
  233. {     
  234.     return oldSeq->LockIndels(FALSE);
  235. }
  236.  
  237. DSequence* DSeqUppercaseCmd::ChangeToNew( DSequence* oldSeq) 
  238. {     
  239.     return oldSeq->ChangeCase(TRUE);
  240. }
  241.  
  242. DSequence* DSeqLowercaseCmd::ChangeToNew( DSequence* oldSeq) 
  243. {     
  244.     return oldSeq->ChangeCase(FALSE);
  245. }
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.         
  253.  
  254.  
  255. // DAlnSlider --------------------------------------
  256.  
  257.  
  258. DAlnSlider::DAlnSlider()
  259. {
  260.     fSlideAll= true;
  261.     fAlnView= NULL;    
  262.     fSeqDoc= NULL;
  263.     fOldSeqs= NULL;
  264.     fNewSeqs= NULL;
  265.     //IAlnSlider( NULL, NULL, 0);    
  266. }
  267.  
  268. DAlnSlider::DAlnSlider( DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView,  short oldRC)
  269. {
  270.     IAlnSlider( itsDoc, itsView, itsAlnView, oldRC);    
  271. }
  272.  
  273. DAlnSlider::~DAlnSlider() 
  274. {
  275.     fNewSeqs->fDeleteObjects= false;
  276.     FreeListIfObject( fNewSeqs);  
  277.     fOldSeqs->fDeleteObjects= true;
  278.     FreeListIfObject( fOldSeqs); //drop all objects && list
  279. #if 0
  280.     fOldSelection= Nlm_DestroyRgn( fOldSelection);
  281.     fNewSelection= Nlm_DestroyRgn( fNewSelection);
  282. #endif
  283. }
  284.  
  285. void DAlnSlider::IAlnSlider(DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView, short oldRC)
  286. {
  287.     fAlnView= itsAlnView;    
  288.     fSeqDoc= itsDoc;
  289.     fOldSeqs= NULL;
  290.     fNewSeqs= NULL;
  291.     fSlideAll= ! (gKeys->shift());
  292.  
  293.     IRCshifter( cAlnShiftCmd, itsDoc, itsView, kDoCol, oldRC); 
  294.         // IRCshifter->ITracker->Reset()  calls our Reset() !
  295. #if 0
  296.     fOldSeqs= new DSeqList();  
  297.     fNewSeqs= new DSeqList();  
  298. #if 1
  299.     fOldSelection= fTable->GetSelRect();
  300.     fNewSelection= fOldSelection;
  301. #else
  302.     fOldSelection= Nlm_NewRgn(); 
  303.     fNewSelection= Nlm_NewRgn();  
  304.     CopyRgn( itsAlnView->fSelections, fOldSelection);
  305.     CopyRgn( fOldSelection, fNewSelection);
  306. #endif
  307. #endif
  308. }
  309.  
  310. void DAlnSlider::Reset()
  311. {
  312.     if (fNewSeqs) {
  313.     fNewSeqs->fDeleteObjects= false;
  314.     FreeListIfObject( fNewSeqs);  
  315.     }
  316.     if (fOldSeqs) {
  317.     fOldSeqs->fDeleteObjects= true;
  318.     FreeListIfObject( fOldSeqs); //drop all objects && list
  319.     }
  320.     fOldSeqs= new DSeqList();  
  321.     fNewSeqs= new DSeqList();  
  322.     if (fTable) fOldSelection= fTable->GetSelRect();
  323.     fNewSelection= fOldSelection;
  324. }
  325.  
  326.  
  327.  
  328. void DAlnSlider::TrackFeedback( TrackPhase aTrackPhase,
  329.                     const Nlm_PoinT& anchorPoint, const Nlm_PoinT& previousPoint,
  330.                     const Nlm_PoinT& nextPoint, Nlm_Boolean mouseDidMove, Nlm_Boolean turnItOn)
  331. {
  332.     short row, col, oldrow, oldcol;
  333.     Nlm_RecT     selr, pixr;
  334.     
  335.     switch (aTrackPhase) {
  336.             
  337.         case trackContinue:
  338.             //mouseDidMove= ((previousPoint.x != nextPoint.x)); //(col != pcol);
  339.         case trackBegin:
  340.         case trackEnd:
  341.             if (mouseDidMove) {
  342.                 fTable->PointToCell( nextPoint, row, col);
  343.                 fTable->PointToCell( anchorPoint, oldrow, oldcol);
  344.                 selr= fOldSelection;
  345.                 if (fRowCol == kDoCol)
  346.                     Nlm_OffsetRect( &selr, col - oldcol, 0);
  347.                 else
  348.                     Nlm_OffsetRect( &selr, 0, row - oldrow);
  349. #if 1
  350.                 fTable->GetCellRect( selr, pixr);
  351.                 Nlm_InvertMode();
  352.                 Nlm_FrameRect( &pixr);
  353.                 Nlm_CopyMode();
  354. #else
  355.                 fTable->SelectCells( selr, 
  356.                         !DTableView::kExtend, DTableView::kHighlight, DTableView::kSelect);
  357. #endif
  358.                 }
  359.             break;
  360.         }
  361. }
  362.  
  363.  
  364. void DAlnSlider::DoSlide()
  365. {
  366.     short     row, col, left, right, top, bottom;
  367.     long        start, nbases, dist;
  368.     DSequence * oldSeq, * newSeq;
  369.      
  370.     dist = fNewRC - fOldRC;
  371.     
  372.     // if (fRowCol == kDoCol)
  373.     Nlm_OffsetRect( &fNewSelection, dist, 0);
  374.     
  375.     //fTable->GetFirstSelectedCell( top, left);
  376.     //fTable->GetLastSelectedCell( bottom, right);
  377.     top= fOldSelection.top;
  378.     bottom= fOldSelection.bottom;
  379.     left= fOldSelection.left;
  380.     right= fOldSelection.right;
  381.     for (row= Max(0,top); row<bottom; row++) {        
  382.         col= left;
  383.         //while (col<right && !fTable->IsSelected(row,col)) col++;
  384.         start= col;
  385.         col= right; 
  386.         //while (col<=right && fTable->IsSelected(row,col)) col++;
  387.             // ! nbases == 0 is key to slide all of sequence from sel to top...
  388.         if (dist<0 && fSlideAll) nbases= 0;
  389.         else nbases= col - start;
  390.         if (nbases>=0) {
  391.             oldSeq= fAlnView->fSeqList->SeqAt(row);
  392.             if (oldSeq) {
  393.                 oldSeq->SetIndex( row);
  394.                 oldSeq->SetSelection( start, nbases);
  395.                 newSeq= oldSeq->Slide( dist); //< ?? DSeqChangeCmd !?
  396.                 oldSeq->ClearSelection();        // 12nov92 - fix for write trunc bug? 
  397.                 if (newSeq) {
  398.                     newSeq->ClearSelection();
  399.                     fOldSeqs->InsertLast(oldSeq);
  400.                     fNewSeqs->InsertLast(newSeq);
  401.                   }
  402.                 }
  403.             }
  404.         }
  405. }
  406.  
  407.  
  408. void DAlnSlider::DoItWork()  
  409. {
  410. /* insert "-" at left/5'/bottom of sequence if dist>0
  411.     squeeze out "-" at top/3' of sequence.
  412.     If dist<0 then slide opposite direction.
  413. */
  414.     Nlm_RecT     r;
  415.     short            zeroshift, left;
  416.     
  417.     if (fMovedOnce) {
  418.         short i, n= fNewSeqs->GetSize();
  419.         for (i=0; i<n; i++) {        
  420.                 // replaceViewSeq
  421.             DSequence* aSeq= fNewSeqs->SeqAt(i);
  422.             fAlnView->fSeqList->AtPut( aSeq->Index(), aSeq);
  423.             fAlnView->GetRowRect( aSeq->Index(), r);
  424.             fAlnView->InvalRect( r);
  425.             }
  426.  
  427.         zeroshift= fAlnView->fSeqList->ZeroOrigin(); //!?
  428.         if (zeroshift) {
  429.                 // THIS is getting messy w/ undo/redo because zeroOrigin affects All seqs!
  430.             fAlnView->UpdateAllWidths();
  431.             left= fNewSelection.left; //(*fNewSelection)->rgnBBox.left;
  432.             if (left + zeroshift <= 0)  zeroshift= -left + 1; //??
  433.             Nlm_OffsetRect( &fNewSelection, zeroshift, 0); 
  434.             fAlnView->Invalidate(); 
  435.             }
  436.  
  437.                 // the inval/redraw will handle selection highlight
  438.         fTable->SelectCells( fNewSelection, 
  439.                 !DTableView::kExtend, !DTableView::kHighlight, DTableView::kSelect);
  440.  
  441. #if 1
  442.         n= fNewSeqs->GetSize();
  443.         for (i= 0; i<n; i++)
  444.             DSeqedWindow::UpdateEdWinds( fOldSeqs->SeqAt(i), fNewSeqs->SeqAt(i) );
  445. #endif
  446.         }
  447. }
  448.  
  449.  
  450. void DAlnSlider::UndoWork()  
  451. {
  452.     DSeqList* tmp= fOldSeqs; 
  453.     fOldSeqs= fNewSeqs;
  454.     fNewSeqs= tmp;
  455. #if 1
  456.     Nlm_RecT tmpRect= fOldSelection;
  457.     fOldSelection= fNewSelection;
  458.     fNewSelection= tmpRect;
  459. #else
  460.     Nlm_RgN tmpRgn= fOldSelection;
  461.     fNewSelection= fOldSelection;
  462.     fOldSelection= tmpRgn;
  463. #endif
  464.     DoItWork();
  465. }
  466.  
  467. void DAlnSlider::DoIt()  
  468. {
  469.     short row, col, oldrow, oldcol;
  470.     
  471.     fTable->PointToCell( fNextPoint, row, col);
  472.     fTable->PointToCell( fAnchorPoint, oldrow, oldcol);
  473.     if (fRowCol == kDoRow) { fOldRC= oldrow; fNewRC= row; }
  474.     else if (fRowCol == kDoCol) { fOldRC= oldcol; fNewRC= col; }
  475.     
  476.     if (fNewRC != fOldRC) DoSlide();
  477.      
  478.     DCommand::DoIt();
  479.     fSeqDoc->Dirty();
  480.     DoItWork();
  481. }
  482.  
  483. void DAlnSlider::Undo()  
  484. {
  485.     DCommand::Undo();
  486.     fSeqDoc->UnDirty();
  487.     UndoWork();
  488. }
  489.  
  490. void DAlnSlider::Redo()  
  491. {
  492.     DCommand::Redo();
  493.     fSeqDoc->Dirty();
  494.     UndoWork();
  495. }
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502. // DAlnShifter --------------------------------
  503.  
  504.  
  505.  
  506. DAlnShifter::DAlnShifter()
  507. {
  508. }
  509.  
  510. DAlnShifter::DAlnShifter( DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView, short oldRC) :
  511.   DAlnSlider( itsDoc, itsView, itsAlnView, oldRC)
  512. {
  513.     DAlnShifter::IAlnSlider( itsDoc, itsView, itsAlnView, oldRC);
  514. }
  515.  
  516. DAlnShifter::~DAlnShifter() 
  517. {
  518. }
  519.  
  520. void DAlnShifter::IAlnSlider( DSeqDoc* itsDoc, DTableView* itsView, DAlnView* itsAlnView, short oldRC)
  521. {
  522.     fAlnView= itsAlnView;    
  523.     fSeqDoc= itsDoc;
  524.     fOldSeqs= NULL;
  525.     fNewSeqs= NULL;
  526.  
  527.     IRCshifter( cAlnShiftCmd, itsDoc, itsView, kDoRow, oldRC);
  528.     fOldSeqs= new DSeqList();  
  529.     fNewSeqs= new DSeqList();  
  530.     fOldSelection= fTable->GetSelRect();
  531.     fNewSelection= fOldSelection;
  532. }
  533.  
  534.  
  535. void DAlnShifter::TrackFeedback( TrackPhase aTrackPhase,
  536.                     const Nlm_PoinT& anchorPoint, const Nlm_PoinT& previousPoint,
  537.                     const Nlm_PoinT& nextPoint, Nlm_Boolean mouseDidMove, Nlm_Boolean turnItOn)
  538. {
  539.     DAlnSlider::TrackFeedback( aTrackPhase, anchorPoint, previousPoint, nextPoint,
  540.                                                         mouseDidMove, turnItOn);
  541. }
  542.  
  543.  
  544. void DAlnShifter::DoSlide()
  545. {
  546. }
  547.  
  548.  
  549. void DAlnShifter::UndoWork()  
  550. {
  551.     DSeqList* tmp= fOldSeqs; 
  552.     fOldSeqs= fNewSeqs;
  553.     fNewSeqs= tmp;
  554.  
  555.     Nlm_RecT tmpRect= fOldSelection;
  556.     fOldSelection= fNewSelection;
  557.     fNewSelection= tmpRect;
  558.  
  559.     short saverc= fNewRC;
  560.     fNewRC= fOldRC;
  561.     fOldRC= saverc;
  562.     
  563.     DoItWork();
  564. }
  565.  
  566. void DAlnShifter::DoItWork()  
  567. {
  568.     short diff = fNewRC - fOldRC;
  569.     if (fMovedOnce && diff) {
  570.         short i, nrows, newtop, maxrow, top, bottom, minr, maxr;
  571.         DSeqList* tmpseq;
  572.         DSequence* aseq;
  573.         Nlm_RecT    r;
  574.         
  575.         tmpseq= new DSeqList();
  576.         top= fOldSelection.top;
  577.         bottom= fOldSelection.bottom;
  578.         nrows= bottom - top;
  579.         if (nrows<1) nrows= 1;
  580.         newtop= top + diff;
  581.         maxrow= fAlnView->fSeqList->GetSize();
  582.         if (newtop<0) {
  583.             newtop=0;
  584.             diff= newtop - top;
  585.             }
  586.         else if (newtop > maxrow-nrows) {
  587.             newtop= maxrow-nrows;
  588.             diff= newtop - top;
  589.             }
  590.             
  591. #if 1
  592.         fNewSelection= fOldSelection;
  593.         fNewSelection.top= newtop;
  594.         fNewSelection.bottom= newtop + nrows;
  595. #else
  596.         if (fRowCol == kDoCol)
  597.             Nlm_OffsetRect( &fNewSelection, diff, 0);
  598.         else
  599.             Nlm_OffsetRect( &fNewSelection, 0, diff);
  600. #endif            
  601.         
  602.         for ( i= 0; i<nrows; i++) {
  603.           aseq= fAlnView->fSeqList->SeqAt( i+top);
  604.             tmpseq->InsertLast(aseq);
  605.             }
  606.         for (i= nrows-1; i>=0; i--) 
  607.           fAlnView->fSeqList->AtDelete( i+top);
  608.         for (i= 0; i<nrows; i++) {
  609.           aseq= tmpseq->SeqAt(i);
  610.           fAlnView->fSeqList->InsertBefore( i+newtop, aseq);
  611.             }
  612.  
  613.         tmpseq->DeleteAll();  
  614.         delete tmpseq;             //drop list, not objects
  615.          
  616.         minr= Min( top, newtop);
  617.         maxr= Max( top, newtop);
  618.         fTable->GetRowRect( minr, r, (maxr-minr) + nrows);
  619.         fTable->InvalRect( r);
  620.         if (fTable != fAlnView) {
  621.             fAlnView->GetRowRect( minr, r, (maxr-minr) + nrows);
  622.             fAlnView->InvalRect( r);
  623.             }
  624.         
  625.         fTable->SelectCells( fNewSelection, 
  626.                 !DTableView::kExtend, !DTableView::kHighlight, DTableView::kSelect);
  627.         }
  628. }
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.         
  636.  
  637. //    DAlnEditCommand 
  638.  
  639. DAlnEditCommand::DAlnEditCommand(DSeqDoc* itsDoc, short itsCommand) :
  640.     DCommand( itsCommand, itsDoc->fAlnView, "edit", DCommand::kCanUndo, DCommand::kCausesChange)
  641. {
  642.     fAlnDoc = itsDoc;
  643.     fOldList= NULL;
  644.     //fChangesClipboard = (itsCommand != DApplication::kClear);
  645.     fCausesChange = (itsCommand != DApplication::kCopy);
  646.     fCanUndo = (itsCommand != DApplication::kCopy);  //?
  647.  
  648.     fOldList= new DSeqList();
  649.     long  i, n= fAlnDoc->fSeqList->GetSize();
  650.     for (i=0; i<n; i++) fOldList->InsertLast( fAlnDoc->fSeqList->At(i));
  651.  
  652.     fOldList->fDeleteObjects= false;
  653.     Nlm_LoadRect( &fSelection, 0,0,0,0);
  654.     if (fAlnDoc->fAlnIndex) fSelection= fAlnDoc->fAlnIndex->GetSelRect();
  655. }
  656.  
  657.  
  658.  
  659. DAlnEditCommand::~DAlnEditCommand()  
  660. {
  661.     //fSelection= Nlm_DestroyRgn( fSelection);
  662.     FreeListIfObject( fOldList); 
  663.     fOldList= NULL;
  664. }
  665.  
  666.  
  667. void DAlnEditCommand::CopySelection()
  668. {
  669.     long    irow, minrow, maxrow;
  670.     DSeqList     *    aSeqList;
  671.     DWindow        * clipWindow;
  672.     DAlnView    * clipView = NULL;
  673.     DSeqDoc        *    clipAlnDoc = NULL;
  674.     
  675. #if 1
  676.         // don't want/need DSeqDoc window for clipview !!?
  677.     aSeqList= new DSeqList();
  678.     //clipWindow= new DWindow(0, NULL, DWindow::document, -5, -5, -50, -20, "Clip"); 
  679.     clipWindow= gClipboardMgr->fClipWindow; //!! contents of fClipWindow are getting trashed
  680. #else    
  681.     clipAlnDoc= new DSeqDoc(DSeqDoc::kSeqdoc,NULL,"clip");   
  682.     aSeqList= clipAlnDoc->fSeqList;
  683.     clipWindow= clipAlnDoc;
  684. #endif
  685.  
  686.     minrow= fSelection.top;
  687.     maxrow= fSelection.bottom;
  688.     for (irow=minrow; irow<maxrow; irow++) {
  689.          DSequence* aseq= fAlnDoc->fSeqList->SeqAt(irow);
  690.          if (aseq) aSeqList->InsertLast( aseq->Clone());
  691.         }
  692.  
  693.     clipView= new DAlnView(0,clipWindow,clipAlnDoc,aSeqList, 120, 100);
  694.     if (clipAlnDoc) clipAlnDoc->fAlnView= clipView;
  695.     else clipView->fOwnSeqlist= true;
  696.  
  697. #if 1
  698.     clipView->GetReadyToShow();
  699. #else
  700.     clipView->UpdateSize();
  701.     clipView->UpdateAllWidths(); 
  702. #endif
  703.  
  704.     //this->ClaimClipboard(clipView);  
  705.     gClipboardMgr->SetClipView(clipView);    
  706. }
  707.  
  708.  
  709.  
  710. void DAlnEditCommand::DeleteSelection()
  711.   long irow, minrow, maxrow;
  712.     long totalRows= fAlnDoc->fAlnView->GetMaxRows(); //fOldList->GetSize();
  713.     DAlnIndex* tab= fAlnDoc->fAlnIndex;
  714.   minrow= fSelection.top;
  715.   maxrow= fSelection.bottom;
  716.     Boolean savedel= fAlnDoc->fSeqList->fDeleteObjects;
  717.     fAlnDoc->fSeqList->fDeleteObjects= false;
  718.     for (irow= maxrow-1; irow>=minrow; irow--)  {
  719.         DSequence* aSeq= fAlnDoc->fSeqList->SeqAt(irow);
  720.         if (aSeq) {
  721.             Nlm_RecT     r;
  722.             aSeq->SetDeleted( true);
  723.             fAlnDoc->fSeqList->AtDelete(irow); 
  724.             fAlnDoc->fAlnView->GetRowRect( irow, r, totalRows-irow+1);
  725.             fAlnDoc->fAlnView->InvalRect( r);
  726.             if (tab) {
  727.                 tab->GetRowRect( irow, r, totalRows-irow+1);
  728.                 tab->InvalRect( r);
  729.                 tab->SelectCells( irow, 0, 
  730.                         !DTableView::kExtend, !DTableView::kHighlight, !DTableView::kSelect);
  731.                 }
  732.             }
  733.         }
  734.     fAlnDoc->fSeqList->fDeleteObjects= savedel;
  735.     fAlnDoc->fAlnView->UpdateSize();
  736. }
  737.  
  738.  
  739. void DAlnEditCommand::RestoreSelection()
  740. {
  741.     long irow, totalRows= fOldList->GetSize();
  742.     long minrow= fSelection.top;
  743.     for (irow=totalRows-1; irow>=0; irow--) {
  744.         DSequence* aSeq= fOldList->SeqAt(irow);
  745.         if (aSeq && aSeq->IsDeleted()) {
  746.             aSeq->SetDeleted( false);
  747.             fAlnDoc->fSeqList->InsertBefore( minrow, aSeq);     
  748.  
  749.             //fAlnDoc->fAlnView->InsRowBefore( minrow, 1, fAlnDoc->fAlnView->fRowHeight);
  750.             //fAlnDoc->fAlnIndex->InsRowBefore( minrow, 1, fAlnDoc->fAlnView-fRowHeight);
  751.             fAlnDoc->fAlnView->ChangeRowSize( fAlnDoc->fAlnView->GetMaxRows(), 1);
  752.             if (fAlnDoc->fAlnIndex) 
  753.                 fAlnDoc->fAlnIndex->ChangeRowSize( fAlnDoc->fAlnIndex->GetMaxRows(), 1);
  754.             }
  755.         }
  756.         
  757.     fAlnDoc->fAlnView->UpdateSize();  
  758.     ReSelect(); 
  759. }
  760.  
  761.  
  762. void DAlnEditCommand::Commit()
  763. {
  764.     long irow, totalRows= fOldList->GetSize();
  765.     Boolean savedel= fOldList->fDeleteObjects;
  766.     fOldList->fDeleteObjects= true;
  767.     for (irow= totalRows-1; irow>=0; irow--) {
  768.         DSequence* aSeq= fOldList->SeqAt(irow);
  769.         if (aSeq && aSeq->IsDeleted())  
  770.             fOldList->AtDelete(irow);
  771.         }
  772.     fOldList->fDeleteObjects= savedel;
  773.     DCommand::Commit(); 
  774. }
  775.  
  776.  
  777. void DAlnEditCommand::ReSelect()
  778. {
  779.     if (fAlnDoc->fAlnIndex)
  780.       fAlnDoc->fAlnIndex->SelectCells( fSelection, 
  781.                 !DTableView::kExtend, DTableView::kHighlight, DTableView::kSelect);
  782. }
  783.  
  784.  
  785. void DAlnEditCommand::DoItWork()
  786. {
  787.     if (fNumber != DApplication::kClear) CopySelection();
  788.     if (fNumber != DApplication::kCopy) DeleteSelection();
  789. }
  790.  
  791.  
  792. void DAlnEditCommand::UndoWork()
  793. {
  794.     if (fNumber != DApplication::kCopy) {
  795.         RestoreSelection();
  796.         //fAlnDoc->fAlnView->ScrollSelectionIntoView(TRUE);
  797.         }
  798. }
  799.  
  800.  
  801. void DAlnEditCommand::RedoWork()
  802. {
  803.     if (fNumber != DApplication::kCopy) {
  804.         DeleteSelection();
  805.         //fAlnDoc->fAlnView->ScrollSelectionIntoView(TRUE);
  806.         }
  807. }
  808.  
  809. void DAlnEditCommand::DoIt()  
  810. {
  811.     DCommand::DoIt();
  812.     fAlnDoc->Dirty();
  813.     DoItWork();
  814. }
  815.  
  816. void DAlnEditCommand::Undo()  
  817. {
  818.     DCommand::Undo();
  819.     fAlnDoc->UnDirty();
  820.     UndoWork();
  821. }
  822.  
  823. void DAlnEditCommand::Redo()  
  824. {
  825.     DCommand::Redo();
  826.     fAlnDoc->Dirty();
  827.     RedoWork();
  828. }
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841. // DAlnPasteCommand 
  842.  
  843.  
  844. DAlnPasteCommand::DAlnPasteCommand(DSeqDoc* itsDoc) :
  845.     DCommand( DApplication::kPaste, itsDoc->fAlnView, "paste", DCommand::kCanUndo, DCommand::kCausesChange)
  846. {
  847.     fAlnDoc = itsDoc;
  848.     fClipList= NULL;
  849.     //fChangesClipboard = false;
  850.  
  851.     short col;
  852.     fInsRow= 0;
  853.     if (fAlnDoc->fAlnIndex) 
  854.         fAlnDoc->fAlnIndex->GetLastSelectedCell( fInsRow, col);
  855.     if (fInsRow == DTableView::kNoSelection) 
  856.         fInsRow= fAlnDoc->fSeqList->GetSize();
  857.  
  858.     DAlnView* clipview= NULL;
  859.   clipview= (DAlnView*) gClipboardMgr->fClipView; // GetClipView();
  860.     if (clipview && clipview->fKind == DAlnView::kindAlnView) {
  861.         long  i, n;
  862.         fClipList= new DSeqList();
  863.         n= clipview->fSeqList->GetSize();
  864.         for (i=0; i<n; i++) {
  865.             DSequence* aSeq= clipview->fSeqList->SeqAt(i);
  866.             fClipList->InsertLast( aSeq->Clone());
  867.             }
  868.         fClipList->fDeleteObjects= true;
  869.     }
  870. }
  871.  
  872.  
  873. DAlnPasteCommand::~DAlnPasteCommand()  
  874. {
  875.     FreeListIfObject( fClipList); //drop all objects && list 
  876.     fClipList= NULL;
  877. }
  878.  
  879.  
  880. void DAlnPasteCommand::DoItWork()
  881. {
  882.     if (fClipList) {
  883.         long irow, nrows= fClipList->GetSize();
  884.         for (irow= nrows-1; irow>=0; irow--) {
  885.             DSequence* aSeq= fClipList->SeqAt(irow);
  886.             aSeq= (DSequence*) aSeq->Clone();
  887.             aSeq->SetDeleted(true);
  888.             
  889.             fAlnDoc->fSeqList->InsertBefore( fInsRow, aSeq);
  890.             //fAlnDoc->fAlnView->InsRowBefore( fInsRow, 1, fAlnDoc->fAlnView->fRowHeight);
  891.             //fAlnDoc->fAlnIndex->InsRowBefore( fInsRow, 1, fAlnDoc->fAlnView.fRowHeight);
  892.             fAlnDoc->fAlnView->ChangeRowSize( fAlnDoc->fAlnView->GetMaxRows(), 1);
  893.             if (fAlnDoc->fAlnIndex)
  894.                 fAlnDoc->fAlnIndex->ChangeRowSize( fAlnDoc->fAlnIndex->GetMaxRows(), 1);
  895.             }
  896.         fAlnDoc->fAlnView->UpdateAllWidths();
  897.         fAlnDoc->fAlnView->UpdateSize(); 
  898.         } 
  899.  
  900.  
  901. void DAlnPasteCommand::UndoWork()
  902.     if (fClipList) {
  903.         long irow, totalRows= fAlnDoc->fSeqList->GetSize();
  904.         Boolean savedel= fAlnDoc->fSeqList->fDeleteObjects;
  905.         fAlnDoc->fSeqList->fDeleteObjects= true;
  906.         for (irow= totalRows-1; irow>=0; irow--) {
  907.             DSequence* aSeq= fAlnDoc->fSeqList->SeqAt(irow);
  908.             if (aSeq && aSeq->IsDeleted()) {
  909.                 Nlm_RecT     r;
  910.                 aSeq->SetDeleted(false);
  911.                 fAlnDoc->fSeqList->AtDelete(irow);
  912.                 fAlnDoc->fAlnView->GetRowRect( irow, r, totalRows-irow+1);
  913.                 fAlnDoc->fAlnView->InvalRect( r);
  914.                 if (fAlnDoc->fAlnIndex) {
  915.                     fAlnDoc->fAlnIndex->GetRowRect( irow, r, totalRows-irow+1);
  916.                     fAlnDoc->fAlnIndex->InvalRect( r);
  917.                     fAlnDoc->fAlnIndex->SelectCells( irow, 0, 
  918.                             !DTableView::kExtend, !DTableView::kHighlight, !DTableView::kSelect);
  919.                     }
  920.                 }
  921.             }
  922.         fAlnDoc->fSeqList->fDeleteObjects= savedel;
  923.         fAlnDoc->fAlnView->UpdateAllWidths();
  924.         fAlnDoc->fAlnView->UpdateSize();  
  925.         }
  926. }
  927.  
  928.  
  929.  
  930. void DAlnPasteCommand::Commit()  
  931. {
  932.     if (fClipList) {
  933.         long i, n= fAlnDoc->fSeqList->GetSize();
  934.         for (i=0; i<n; i++) {
  935.             DSequence* aseq= fAlnDoc->fSeqList->SeqAt(i);
  936.             aseq->SetDeleted(false);
  937.             }
  938.         }
  939.     DCommand::Commit(); 
  940. }
  941.  
  942.  
  943. void DAlnPasteCommand::DoIt()  
  944. {
  945.     if (fClipList) {
  946.     DCommand::DoIt();
  947.     fAlnDoc->Dirty();
  948.     DoItWork();
  949.     }
  950. }
  951.  
  952. void DAlnPasteCommand::Undo()  
  953. {
  954.     if (fClipList) {
  955.     DCommand::Undo();
  956.     fAlnDoc->UnDirty();
  957.     UndoWork();
  958.     }
  959. }
  960.  
  961. void DAlnPasteCommand::Redo()  
  962. {
  963.     if (fClipList) {
  964.     DCommand::Redo();
  965.     fAlnDoc->Dirty();
  966.     DoItWork();
  967.     }
  968. }
  969.  
  970.  
  971.  
  972.  
  973.